<?php

namespace Common\Model;

use Think\Model;

class VUserModel extends Model
{
    public function get_list($search_key, $status, $rank, $attribute,$type, $page_no = 1, $page_size = 0)
    {
        $condition = $this->assembleCondition($search_key, $status, $rank, $attribute,$type);
        $count = 0;

        if ($page_size > 0) {
            $list = $this->where($condition)->order('id asc')->limit(($page_no - 1) * $page_size, $page_size)->select();
            $count = $this->where($condition)->count();
        } else {
            $list = $this->where($condition)->order('id asc')->select();
        }
        return [$list, $count];
    }

    private function assembleCondition($search_key, $status, $rank, $attribute,$type)
    {
        $condition = array();
        if (!empty($search_key)) {
            $condition['account'] = array('like', '%' . $search_key . '%');
        }
        if (!empty($type)) {
            $condition['role'] = array('eq', $type);
        }
        if ($status != "" && $status != null) {
            $condition['status'] = array('eq', $status);
        } else {
            $condition['status'] = array('egt', 0);
        }

        if ($rank != "" && $rank != null) {
            $condition['rank'] = array('eq', $rank);
        }

        if ($attribute != "" && $attribute != null) {
            $condition['attribute'] = array('eq', $attribute);
        }
        return $condition;
    }
}